/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fait.ferm.persists;

import fait.ferm.global.Glob;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Greg
 */
public class Staff_table extends PersistsObject {
    public Ferm ferm;
    public String name;
    public double salary;
    public Staff_type staff_type;
    
    public Staff_table(Staff_type newMan) throws Exception {
        super();
        ferm = Glob.getFerm();
        table = "staff_table";
        name = newMan.job;
        staff_type = newMan;
        salary = newMan.salary;
    }
    
    public Staff_table(int pId) throws Exception {
        super(pId);
        ferm = Glob.getFerm();
        table = "staff_table";
        load();
    }
    
    @Override
    public void load() throws Exception {
        boolean notSuccessfully=true;
		if(id==-1) {
			throw new Exception("Объект "+table+" отсутствует в базе");
		}	
		try (Statement st = con.createStatement()) {
			String q = "SELECT ferm_id, name, staff_type_id, salary FROM "+table+" WHERE id = " + id;
			ResultSet rs = st.executeQuery(q);
			if(rs.next()){
				notSuccessfully=false;
                if(rs.getInt("ferm_id") != ferm.getId()) {
                    throw new Exception("Объект не из этой игры");
                }
                name = rs.getString("name");
                staff_type = new Staff_type(rs.getInt("staff_type_id"));
				salary = rs.getDouble("salary");
			}
			rs.close();
		}
		if(notSuccessfully) {
			throw new Exception("Объект "+table+" не загружен");
        }
    }

    public void delete() throws SQLException {
        try (Statement st = con.createStatement()) {
			String q = "DELETE FROM "+table+" WHERE id="+id;
			st.executeUpdate(q);
		}
    }
    
    @Override
    protected void update() throws SQLException {
        try (Statement st = con.createStatement()) {
			String q = "UPDATE "+table+" SET ferm_id="+ferm.getId()+
					", name= \""+name+ "\"" +
                    ", staff_type_id="+staff_type.getId()+
					", salary = "+ salary + " " +
					"WHERE id="+id;
			st.executeUpdate(q);
		}
    }

    @Override
    protected void createNew() throws SQLException, Exception {
        try (Statement st = con.createStatement()) {
			String q = "INSERT INTO "+table+" "+
					"(ferm_id, name, staff_type_id, salary) VALUES("+
						Glob.getFerm().id + ","+
						"\"" + name + "\","+
                        staff_type.getId() + ","+
						salary +
					")";
			st.executeUpdate(q);
			q="SELECT seq FROM sqlite_sequence WHERE name = '"+table+"'";
			ResultSet rs = st.executeQuery(q);
			if(rs.next()){
				id = rs.getInt("seq");
			}
			rs.close();
		}
    }
    
    @Override
	public String toString(){
		return name + "(" + Glob.getDirInCache("skill").get(staff_type.skill) + " квалификация)";
	}
    
}
